# -*- coding: utf-8 -*-

import math
import scrapy
import re
from apps.tax_policy.tax_policy.items import NetTaxPolicyItem as Item, urllib, time, urljoin, json
from apps.tax_policy.tax_policy.spiders.base_spider.base_tax_policy_spider import BaseTaxPolicySpider


class SichuanLeshanPolicy(BaseTaxPolicySpider):
    name = 'sichuan_leshan_policy'

    province = '四川省'
    city = '乐山市'
    # county = ''
    park = ''

    def __init__(self, get_next=False, **kwargs):
        super().__init__(**kwargs)
        print(get_next)
        self.get_next = get_next

    @classmethod
    def update_settings(cls, settings) -> None:
        new_settings = {
            **(cls.custom_settings or {}),
            **{
                # "COOKIES_ENABLED": False,
                "DOWNLOAD_TIMEOUT": 200,
                # "CONCURRENT_REQUESTS": 2,
                "HTTPERROR_ALLOWED_CODES": [302, 400, 404, 500, 200, 202, 502, 429, 403],
                # "DOWNLOAD_DELAY": 5,
                # "DOWNLOADER_MIDDLEWARES": {
                #     "components.middlewares.downloadmiddlewares.public.company_ip_by_api.CompanyIpByApiMiddleware": None,
                # },
            },
        }
        settings.setdict(new_settings or {}, priority="spider")

    def start_requests(self):
        for source, county, url in [
            ['乐山市人民政府', "", "https://www.leshan.gov.cn/lsswszf/gfxwj/glist.shtml"],
            ['乐山市住房和城乡建设局', "", "https://szjj.leshan.gov.cn/szfcxjsj/xzgfxwj/fdzdgknr.shtml"],
            ['乐山市人力资源和社会保障局', "", "http://lsrsj.leshan.gov.cn/srsj/zcfg/list.shtml"],
            ['乐山市市场监督管理局', "", "https://scjgj.leshan.gov.cn/scjgj/gfxwj/xlist.shtml"],
            ['乐山市市中区人民政府', "", "http://www.lsszq.gov.cn/szq/xzgfxwj2/jcxxgklist.shtml"],
            ['乐山市沙湾区人民政府', "沙湾区", "http://www.shawan.gov.cn/swq/zcwj/glist.shtml"],
            ['乐山市五通桥区人民政府', "五通桥区", "http://www.wtq.gov.cn/wtqq/xzgfxwj2/jcxxgklist.shtml"],
            ['乐山市金口河区人民政府', "金口河区", "http://www.jkh.gov.cn/jkhq/xzgfxwj/list.shtml"],
            ['乐山市金口河区人民政府', "金口河区", "http://www.jkh.gov.cn/jkhq/jff/list.shtml"],
            ['乐山市金口河区人民政府', "金口河区", "http://www.jkh.gov.cn/jkhq/jfbf/list.shtml"],
            ['乐山市井研县人民政府', "井研县", "http://www.jingyan.gov.cn/jyx/xzgfxwj/list.shtml"],
            ['乐山市井研县人民政府', "井研县", "http://www.jingyan.gov.cn/jyx/zfwj/list.shtml"],
            ['乐山市沐川县人民政府', "沐川县", "http://www.muchuan.gov.cn/mcx/xzgfxwj/list.shtml"],
            ['乐山市沐川县人民政府', "沐川县", "http://www.muchuan.gov.cn/mcx/zfwj/list.shtml"],
            ['乐山市马边彝族自治县人民政府', "马边彝族自治县", "http://www.mabian.gov.cn/mbyzzzx/xzgfxwj2/jcxxgklist.shtml"],
            ['乐山市峨眉山市人民政府', "峨眉山市", "http://www.emeishan.gov.cn/emss/xzgfxwj2/jcxxgklist.shtml"],
            ['乐山市犍为县人民政府', "犍为县", "http://www.qianwei.gov.cn/qwx/xzgfxwj2/jcxxgklist.shtml"],
        ]:
            item = {'source': source, 'county': county}
            yield scrapy.Request(url, callback=self.parse_list, meta={'item': item}, dont_filter=True)

    def parse_list(self, response, **kwargs):
        prev_item = response.meta.get('item')
        for elem in response.xpath(
                "//tr[not(@id)]/td[not(@id)]//a[contains(@href, 'htm') and @title]/ancestor::td[string-length(string(.))>=5] | "
                "//div[@class='ArticleList']/p//a[contains(@href, 'htm') or @title]/ancestor::p[string-length(string(.))>=5] | "
                "//div[contains(@class, 'list')]//li//a[contains(@href, 'htm') or @title]/ancestor::li[string-length(string(.))>=5] | "
                "//div[@class='reclist']/dl//a[contains(@href, 'htm') or @title]/ancestor::dl[string-length(string(.))>=5] | "
                "//ul[@id or contains(@class, 'list')]/li//a[contains(@href, 'htm') and @title]/ancestor::li[string-length(string(.))>=10]"
                ):
            item = Item()
            item['source_url'] = elem.xpath(""".//a/@href""").re_first(r"""(.*/.*)""")
            item['source_url'] = response.urljoin(item['source_url'])
            item['publish_date'] = elem.xpath("""string(.)""").get()
            if prev_item is not None:
                for key, value in prev_item.items():
                    item[key] = value
            if not item['source_url']:
                continue
            if '.htm' not in item['source_url'] and '.shtm' not in item['source_url'] and '.jhtm' not in item['source_url']:
                continue
            print(response.url, item['source_url'])
            yield response.follow(item['source_url'], callback=self.parse_detail, meta={'item': item})
        if response.meta.get("is_next") is not False:
            total_page = response.xpath("//script").re_first(r"""createPageHTML\('page_?-?div',(\d+),""") or 0
            print(response.url, "page:", total_page)
            url = response.url
            # if 'index' not in response.url:
            #     url += 'index.html'
            for page_num in range(1, int(int(total_page) / 1 + 1)):
                yield response.follow(url.replace('.shtml', f'_{page_num}.shtml'),
                                      callback=self.parse_list, meta={'item': prev_item, 'is_next': False})

    def parse_detail(self, response, **kwargs):
        item = Item() if response.meta.get('item') is None else response.meta.get('item')
        item['title'] = response.xpath("""string(//meta[@name='ArticleTitle']/@content)""").get()
        item['publish_date'] = response.xpath(
            """string(//meta[@name='PubDate']/@content)""").get() or response.xpath(
            "//*[@class='fbrq']/text()").get()
        item['content'] = response.xpath(""".""").get()
        # item['source'] = self.source
        item['province'] = self.province
        item['city'] = self.city
        # item['county'] = self.county
        item['park'] = self.park
        yield item


if __name__ == "__main__":
    from scrapy import cmdline

    cmdline.execute("scrapy crawl sichuan_leshan_policy -a get_next=0".split())
